package ui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;

import seg.MergeAfter;
import seg.RecurSeg;
import seg.cd.CornerDetector;
import doc.Stroke;

/**
 *
 * @author  zhengwt
 */
public class StrokeViewer extends javax.swing.JFrame {

    public StrokeViewer() {
        initComponents();

    }

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(getJPanelSketch(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(getJPanelCommand(), javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(getJPanelSketch(), javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(getJPanelCommand(), javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();

        setResizable(false);
        setLocation(120, 80);
        getJCheckBoxShowPoints().setSelected(true);
        getJComboBoxK().setSelectedIndex(2);

//        Threshold.train();
    }

    public static void main(String args[]) {
    	try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StrokeViewer().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonFiltrate;
    private javax.swing.JButton jButtonLoad;
    private javax.swing.JButton jButtonMerge;
    private javax.swing.JButton jButtonPre;
    private javax.swing.JCheckBox jCheckBoxShowPoints;
    private javax.swing.JComboBox jComboBoxGap;
    private javax.swing.JComboBox jComboBoxK;
    private javax.swing.JPanel jPanelCommand;
    private javax.swing.JPanel jPanelSketch;
    private javax.swing.JScrollPane jScrollPanePoints;
    private javax.swing.JTree jTreePoints;

	public javax.swing.JButton getJButtonClear() {
		if (jButtonClear == null) {
			jButtonClear = new JButton();
			jButtonClear.setText("Clear");
		}
		return jButtonClear;
	}

	public javax.swing.JButton getJButtonFiltrate() {
		if (jButtonFiltrate == null) {
			jButtonFiltrate = new JButton();
			jButtonFiltrate.setText("Filtrate");

			jButtonFiltrate.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					RecurSeg rs = RecurSeg.get();
					rs.selectCD(CornerDetector.TYPE.ThetaSigma);

					rs.setK(getJComboBoxK().getSelectedIndex() + 1);
					rs.setPoints(Stroke.get().getPoints());

					rs.segment();
					Stroke.get().setCorners(rs.getCorners());

					getJPanelSketch().repaint();
				}

			});
		}
		return jButtonFiltrate;
	}

	public javax.swing.JButton getJButtonLoad() {
		if (jButtonLoad == null) {
			jButtonLoad = new JButton();
			jButtonLoad.setText("Load");

			jButtonLoad.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					int result = getJFileChooser().showOpenDialog(StrokeViewer.this);
					if (result == JFileChooser.APPROVE_OPTION) {
						Stroke.get().readFromFile(getJFileChooser().getSelectedFile());
						getJPanelSketch().repaint();
					}
				}

			});
		}
		return jButtonLoad;
	}

	public javax.swing.JButton getJButtonMerge() {
		if (jButtonMerge == null) {
			jButtonMerge = new JButton();
			jButtonMerge.setText("Merge");

			jButtonMerge.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					MergeAfter.get().merge();
					getJPanelSketch().repaint();
				}

			});
		}
		return jButtonMerge;
	}

	public javax.swing.JButton getJButtonPre() {
		if (jButtonPre == null) {
			jButtonPre = new JButton();
			jButtonPre.setText("Preprocess");

			jButtonPre.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					Stroke.get().preprocess();

					getJPanelSketch().repaint();
				}

			});
		}
		return jButtonPre;
	}

	public javax.swing.JCheckBox getJCheckBoxShowPoints() {
		if (jCheckBoxShowPoints == null) {
			jCheckBoxShowPoints = new JCheckBox();
			jCheckBoxShowPoints.setText("show points");
	        jCheckBoxShowPoints.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
	        jCheckBoxShowPoints.setMargin(new java.awt.Insets(0, 0, 0, 0));
	        jCheckBoxShowPoints.setSelected(false);

	        jCheckBoxShowPoints.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					getJPanelSketch().repaint();
				}

	        });
		}
		return jCheckBoxShowPoints;
	}

	public javax.swing.JComboBox getJComboBoxGap() {
		if (jComboBoxGap == null) {
			jComboBoxGap = new JComboBox();
//			jComboBoxGap.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));
			jComboBoxGap.setEnabled(false);
		}
		return jComboBoxGap;
	}

	public javax.swing.JComboBox getJComboBoxK() {
		if (jComboBoxK == null) {
			jComboBoxK = new JComboBox();
			String[] ss = new String[20];
			for (int i = 0; i < ss.length; i++) {
				ss[i] = Integer.toString(i+1);
			}
			jComboBoxK.setModel(new javax.swing.DefaultComboBoxModel(ss));
		}
		return jComboBoxK;
	}

	public javax.swing.JPanel getJPanelCommand() {
		if (jPanelCommand == null) {
			jPanelCommand = new JPanel();
			jPanelCommand.setBorder(javax.swing.BorderFactory.createTitledBorder("Command"));

			javax.swing.GroupLayout jPanelCommandLayout = new javax.swing.GroupLayout(jPanelCommand);
	        jPanelCommand.setLayout(jPanelCommandLayout);
	        jPanelCommandLayout.setHorizontalGroup(
	            jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                .addGap(20, 20, 20)
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                        .addComponent(getJButtonFiltrate())
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(getJComboBoxK(), 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	                    .addComponent(getJButtonPre())
	                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                        .addComponent(getJButtonMerge())
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(getJComboBoxGap(), javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                .addContainerGap(19, Short.MAX_VALUE))
	            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                        .addGap(10, 10, 10)
	                        .addComponent(getJCheckBoxShowPoints()))
	                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                        .addComponent(getJButtonLoad())
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(getJButtonClear())))
	                .addContainerGap(98, Short.MAX_VALUE))
	            .addComponent(getJScrollPanePoints(), javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
	        );
	        jPanelCommandLayout.setVerticalGroup(
	            jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(getJButtonLoad())
	                    .addComponent(getJButtonClear()))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(getJCheckBoxShowPoints())
	                .addGap(30, 30, 30)
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(getJButtonPre())
	                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                        .addGap(29, 29, 29)
	                        .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(getJButtonFiltrate())
	                            .addComponent(getJComboBoxK(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(getJButtonMerge())
	                    .addComponent(getJComboBoxGap(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
	                .addComponent(getJScrollPanePoints(), javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE))
	        );
		}
		return jPanelCommand;
	}

	public javax.swing.JPanel getJPanelSketch() {
		if (jPanelSketch == null) {
			jPanelSketch = new JPanel() {

				public void paint(Graphics g) {
					super.paint(g);


					Graphics2D g2 = (Graphics2D) g;
//
//					new Rect(100,200,100,200,Math.PI * .5).draw(g2);
					Stroke.get().draw(g2);
					if (getJCheckBoxShowPoints().isSelected())
						Stroke.get().drawPoints(g2);
					Stroke.get().drawCorners(g2);

//					Ellipse el = new Ellipse();
//					el.setCenter(new Point(410.59, 311.35));
//					el.setMajor(56.27);
//					el.setMinor(0.917);
//					el.setAngle(-0.0474);
//
//					Point p1 = new Point(380,313);
//					Point p2 = new Point(351,314);
//
//					g2.drawOval(p1.intX()-2, p1.intY()-2, 4, 4);
//					g2.drawOval(p2.intX()-2, p2.intY()-2, 4, 4);
//					el.draw(g2);
//					System.err.println(el.ptEllipseDist(p1));
//					System.err.println(el.ptEllipseDist(p2));
				}

			};

			jPanelSketch.setBackground(new java.awt.Color(255, 255, 255));
	        javax.swing.GroupLayout jPanelSketchLayout = new javax.swing.GroupLayout(jPanelSketch);
	        jPanelSketch.setLayout(jPanelSketchLayout);
	        jPanelSketchLayout.setHorizontalGroup(
	            jPanelSketchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 523, Short.MAX_VALUE)
	        );
	        jPanelSketchLayout.setVerticalGroup(
	            jPanelSketchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 530, Short.MAX_VALUE)
	        );
		}
		return jPanelSketch;
	}

	public javax.swing.JScrollPane getJScrollPanePoints() {
		if (jScrollPanePoints == null) {
			jScrollPanePoints = new JScrollPane();
			jScrollPanePoints.setViewportView(getJTreePoints());
		}
		return jScrollPanePoints;
	}

	public javax.swing.JTree getJTreePoints() {
		if (jTreePoints == null) {
			jTreePoints = new JTree();
		}
		return jTreePoints;
	}

	public JFileChooser getJFileChooser() {
		if (jFileChooser == null) {
			jFileChooser = new JFileChooser();
			try {
				jFileChooser.setCurrentDirectory(new File(new File("C:\\Workspace\\Exp\\segmentation\\sample\\zhengwt").getCanonicalPath()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return jFileChooser;
	}

	private JFileChooser jFileChooser;

}
